﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Subgurim.Controles;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace StoreLocatorAWC
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                Initialize();   
        }
        public void Initialize()
        {
            GMap1.Height = 500;
            GMap1.Width = 900;
            GMap1.GZoom = 3;
            GLatLng center = new GLatLng(39.011902, -98.4842465);
            GMap1.setCenter(center);

            GMapUIOptions options = new GMapUIOptions();
            options.controls_largemapcontrol3d = true;
            options.controls_scalecontrol = true;
            options.zoom_scrollwheel = true;
            options.controls_scalecontrol = true;
            options.keyboard = true;
            GMap1.Add(new GMapUI(options));

            MarkerManager markerManager = new MarkerManager();
            

            List<Store> StoresList = Store.GetStoresData();
            foreach (Store store in StoresList)
            {
                Double lat = Convert.ToDouble(store.lat.ToString());
                Double lng = Convert.ToDouble(store.lng.ToString());
                GLatLng point = new GLatLng(lat, lng);
                GIcon icon3 = new GIcon();
                icon3.flatIconOptions = new FlatIconOptions(25, 25, Color.Red, Color.Black, "B", Color.White, 15,
                                                            FlatIconOptions.flatIconShapeEnum.roundedrect);
                GMarker marker = new GMarker(point, icon3);

                GInfoWindow info = new GInfoWindow(marker, "Store Name: " + store.storeName.ToString()
                                                        + "<br/> Address: " + store.Address1.ToString()
                                                        + "<br/><a href=\"http://maps.google.nl/maps?saddr=" + TxtAddress.Text + "&daddr=" + store.Address1.ToString()+" "+ store.city.ToString() +" "+ store.state.ToString() + "\">Toon Route</a>");
                //GMap1.addGMarker(marker);
                //GMap1.Add(info);
                //markerManager.Add(marker, 6);
                markerManager.Add(info, 5);
            }
            GMap1.markerManager = markerManager;
        }

        protected void BtnFind_Click(object sender, EventArgs e)
        {
            string Key = System.Configuration.ConfigurationManager.AppSettings.Get("googlemaps.subgurim.net");
            string addressInput = TxtAddress.Text;
            int radiusInput = Convert.ToInt16(RadiusList.SelectedValue);

            GeoCode geoCode = GMap.geoCodeRequest(addressInput, Key);
            Double latt = geoCode.Placemark.coordinates.lat;
            Double longg = geoCode.Placemark.coordinates.lng;
            GLatLng point = new GLatLng(latt, longg);
            GMarker marker = new GMarker(new GLatLng(latt, longg));
            GMap1.addGMarker(marker);
            GInfoWindow info = new GInfoWindow(marker, "Your Location:" + addressInput);
            GMap1.Add(info);
            GMap1.setCenter(point, radiusInput);
        }
        
    }
}
